try
    (* get input file and program arguments *)
    let cmd, file = match Array.to_list Sys.argv with
    [] -> failwith ("Check for sanity")
    | _::[] -> failwith ("No input file")
    | c::f::_ -> c, f
    in

    (* open code file *)
    let input = 
        try open_in file with
        Sys_error(_) -> failwith ("Couldn't open file: " ^ file)
    in

    (* create output buffer *)
    let output =
        let outname = (
            let base = Filename.basename file in
            try Filename.chop_extension base with
            Invalid_argument(_) -> base ) ^ ".ml"
        in
        try open_out outname with
        Sys_error(_) -> 
            failwith ("Couldn't open output file: " ^ outname)
    in

    (* scan, parse, "compile" *)
    let lexbuf = Lexing.from_channel input in
    let ast = Parser.program Scanner.token lexbuf in
    let sast = Check.chk ast in
    let ret_tp, param_tp = Compile.translate sast output in
    Main.prt output ret_tp param_tp
with
Failure(str) -> print_endline ("Error: " ^ str); exit 1

